package com.javabasic.algorithm.leetcode;

import org.junit.Test;

import java.util.Arrays;

/**
 * @author mir.xiong
 * @version 1.0
 * @description
 * @see {EASY} -- [1995. Count Special Quadruplets](https://leetcode-cn.com/problems/count-special-quadruplets/)
 * @since Created by work on 2021/12/29 12:37 上午
 */
public class CountSpecialQuadruplets {

    @Test
    public void test() {
        int[] arr = new int[]{28,8,49,85,37,90,20,8};
        int num = countQuadruplets(arr);
        System.out.println(num);
    }

    /**
     * 暴力法
     * 注意：在数据量稍大的时候就会TTL
     *
     * @param nums
     * @return
     */
    public int countQuadruplets(int[] nums) {
        int len = nums.length, result = 0;
        for (int i = 0; i < len - 3; i++) {
            for (int j = i + 1; j < len - 2; j++) {
                for (int p = j + 1; p < len - 1; p++) {
                    for (int q = p + 1; q < len; q++) {
                        if (nums[i] + nums[j] + nums[p] == nums[q]) {
                            System.out.println(nums[i] + " " + nums[j] + " " + nums[p] + " " + nums[q]);
                            result++;
                        }
                    }
                }
            }
        }
        return result;
    }


    public int countQuadruplets2(int[] nums) {
        int len = nums.length, result = 0;
        for (int i = 0; i < len - 3; i++) {
            for (int j = i + 1; j < len - 2; j++) {
                for (int p = j + 1; p < len - 1; p++) {
                    for (int q = p + 1; q < len; q++) {
                        if (nums[i] + nums[j] + nums[p] == nums[q]) {
                            System.out.println(nums[i] + " " + nums[j] + " " + nums[p] + " " + nums[q]);
                            result++;
                        }
                    }
                }
            }
        }
        return result;
    }
}
